﻿using Esri.ArcGISRuntime.Geometry;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WRJClient.UI.Map
{
    public static class ArcCommon
    {
        public static double GetMotionAngle(double x1, double y1, double x2, double y2)
        {
            double angle = Math.Atan2(y2 - y1, x2 - x1);
            return 90 - (180 / Math.PI) * angle;
        }
        public static Esri.ArcGISRuntime.Geometry.PointCollection GetCyclePoints(MapPoint point, double r)
        {
            point = (MapPoint)GeometryEngine.Project(point, SpatialReferences.WebMercator);
            Esri.ArcGISRuntime.Geometry.PointCollection points = new Esri.ArcGISRuntime.Geometry.PointCollection(SpatialReferences.WebMercator);
            double degree;
            int n = 10;
            r = r * 1000;
            for (int i = 0; i < 360; i += n)
            {
                degree = i * (Math.PI / 180);
                double pointX = point.X + Math.Cos(degree) * r;
                double pointY = point.Y + Math.Sin(degree) * r;
                MapPoint p = new MapPoint(pointX, pointY, SpatialReferences.WebMercator);
                points.Add(p);
            }
            if (points.Count > 0)
            {
                points.Add(points.ElementAt(0));
            }
            return points;
        }

        public static Esri.ArcGISRuntime.Geometry.PointCollection PointCollectionFromGeoPointJson(string path)
        {
            Esri.ArcGISRuntime.Geometry.PointCollection list = new Esri.ArcGISRuntime.Geometry.PointCollection(SpatialReferences.Wgs84);
            try
            {
                List<GeoPoint> points = JsonConvert.DeserializeObject<List<GeoPoint>>(path);
                if (points != null)
                {
                    foreach (var item in points)
                    {
                        list.Add(new MapPoint(item.longitude, item.latitude, SpatialReferences.Wgs84));
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return list;
        }

    }
}
